package com.git.hui.yinshua.biz.order.dao.repository;

import com.git.hui.yinshua.api.model.ReqInfoContext;
import com.git.hui.yinshua.api.model.constants.GlobalConstants;
import com.git.hui.yinshua.api.model.order.PtOrderStatusEnum;
import com.git.hui.yinshua.biz.order.dao.entity.PtOrderEntity;
import com.git.hui.yinshua.biz.order.dao.entity.PtOrderItemEntity;
import com.git.hui.yinshua.biz.order.dao.mapper.PtOrderMapper;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.GoodsInfoDto;
import com.git.hui.yinshua.biz.rest.models.order.req.OrderCreateReq;
import com.git.hui.yinshua.biz.rest.models.order.req.PtOrderQueryReq;
import com.git.hui.yinshua.biz.util.PriceUtil;
import com.git.hui.yinshua.common.DateUtil;
import com.git.hui.yinshua.common.id.OrderIdGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.List;

/**
 * 拼团单
 *
 * @author YiHui
 * @date 2024/7/2
 */
@Repository
public class PtOrderRepository {
    @Autowired
    private PtOrderMapper ptOrderMapper;

    /**
     * 创建拼团订单
     *
     * @param req
     * @param goods
     * @return
     */
    public PtOrderEntity addOrder(GoodsInfoDto goods, OrderCreateReq req) {
        PtOrderEntity entity = new PtOrderEntity();
        entity.setCode("PT" + OrderIdGen.genOrderId());
        entity.setUid(req.getUserId());
        entity.setTitle(ReqInfoContext.getReqInfo().getUser().getNickName() + "创建的【" + goods.getName() + "】拼团" + DateUtil.getToday());
        entity.setGoodsId(goods.getGoodsId());
        entity.setPtPrice(PriceUtil.toCentPrice(goods.getDiscountPrice()));
        entity.setPtNum(goods.getPtNum());
        entity.setExpireMin(goods.getPtTimePeriod());
        entity.setPtStatus(PtOrderStatusEnum.INIT.getStatus());

        entity.setStartTime(new Timestamp(System.currentTimeMillis()));
        entity.setEndTime(new Timestamp(entity.getStartTime().getTime() + entity.getExpireMin() * 60_000L));

        entity.setDeleted(GlobalConstants.NOT_DELETED_TAG);
        ptOrderMapper.savePtOrder(entity);
        return entity;
    }

    /**
     * 参团
     *
     * @param req
     * @return
     */
    public PtOrderItemEntity addOrderItem(OrderCreateReq req) {
        PtOrderItemEntity item = new PtOrderItemEntity();
        item.setPtOrderId(req.getPtOrderId());
        item.setRelatedOrderId(req.getOrderId());
        item.setUid(req.getUserId());
        item.setNickname(ReqInfoContext.getReqInfo().getUser().getNickName());
        item.setAvatar(ReqInfoContext.getReqInfo().getUser().getAvatar());
        item.setPtStatus(PtOrderStatusEnum.INIT.getStatus());
        item.setParticipationTime(new Timestamp(System.currentTimeMillis()));
        item.setDeleted(GlobalConstants.NOT_DELETED_TAG);
        ptOrderMapper.savePtOrderItems(item);
        return item;
    }

    /**
     * 拼团单查询
     *
     * @param ptOrderId
     * @return
     */
    public PtOrderEntity queryPtOrderForLock(Integer ptOrderId) {
        return ptOrderMapper.selectPtOrderAndLock(ptOrderId);
    }

    /**
     * 拼团单查询
     *
     * @param orderId 订单id
     * @return 订单对应的拼团信息
     */
    public PtOrderEntity queryPtOrderByOrderId(Long orderId) {
        Integer ptOrderId = ptOrderMapper.queryPtOrderIdByOrderId(orderId);
        if (ptOrderId != null) {
            return ptOrderMapper.selectPtOrder(ptOrderId);
        }
        return null;
    }


    /**
     * 更新拼团订单状态
     *
     * @param ptOrder
     * @param ptOrderStatus
     * @return
     */
    public boolean updateAllPtOrderStatus(Integer ptOrder, Integer ptOrderStatus) {
        return ptOrderMapper.updatePtSucceedStatus(ptOrder, ptOrderStatus) > 0;
    }

    /**
     * 更新拼团成员订单状态
     *
     * @param relatedOrderId
     * @param ptStatus
     * @return
     */
    public boolean updatePtItemStatus(Long relatedOrderId, Integer ptStatus) {
        return ptOrderMapper.updatePtItemPayStatus(relatedOrderId, ptStatus) > 0;
    }

    public boolean updatePtOrderStatus(Integer ptId, Integer ptStatus) {
        return ptOrderMapper.updatePtOrderPayStatus(ptId, ptStatus) > 0;
    }

    /**
     * 查询超时没有成团的拼团单，用于自动关单
     *
     * @return
     */
    public List<Integer> queryOverTimedPtOrders() {
        return ptOrderMapper.queryOverTimeFailedOrders();
    }

    /**
     * 查询拼团列表
     *
     * @param req
     * @return
     */
    public List<PtOrderEntity> listOrder(PtOrderQueryReq req) {
        if (CollectionUtils.isEmpty(req.getState())) {
            req.setState(null);
        }
        return ptOrderMapper.listOrder(req);
    }

    /**
     * 查询拼团单关联的所有订单
     *
     * @param code 拼团code
     * @return
     */
    public List<PtOrderItemEntity> listOrderByCode(String code) {
        return ptOrderMapper.listPtOrderItemsByCode(code);
    }
}
